home *** CD-ROM | disk | FTP | other *** search
/ Aminet 23 / Aminet 23 (1998)(GTI - Schatztruhe)[!][Feb 1998].iso / Aminet / disk / misc / TransADF.lha / Source / main.c < prev    next >
C/C++ Source or Header  |  1997-12-06  |  9KB  |  386 lines

  1. /*----------------*/
  2. /* Main procedure */
  3. /*----------------*/
  4.  
  5. /* COMPILE_LITE takes precedence over COMPILE_RT */
  6. #ifdef COMPILE_LITE
  7. #  ifdef COMPILE_RT
  8. #    undef COMPILE_RT
  9. #  endif /* COMPILE_RT */
  10. #endif /* COMPILE_LITE */
  11.  
  12. #include <exec/types.h>
  13. #include <exec/ports.h>
  14. #include <exec/io.h>
  15. #ifdef COMPILE_RT
  16. #  include <exec/libraries.h>
  17. #endif /* COMPILE_RT */
  18. #include <devices/trackdisk.h>
  19. #include <dos/dos.h>
  20. #include <dos/dosextens.h>
  21. #include <dos/rdargs.h>
  22. #include <clib/exec_protos.h>
  23. #include <clib/dos_protos.h>
  24.  
  25. #include <fcntl.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28.  
  29. #include "main.h"
  30. #include "read_disk.h"
  31. #include "write_disk.h"
  32. #ifndef COMPILE_LITE
  33. #  include "defl_disk.h"
  34. #  include "infl_disk.h"
  35. #endif /* COMPILE_LITE */
  36. #include "util.h"
  37. #include "errors.h"
  38. #include "version.h"
  39.  
  40.  
  41. /*---------------------------------*/
  42. /* Global constants and structures */
  43. /*---------------------------------*/
  44.  
  45. const char breakText[]   = "***Break";
  46. const char TA_Template[] = "DRIVE/A,FILE/A,S=START/N,E=END/N,WRITE/S,\
  47. ZLIB/S,GZIP/S,PKZIP/S,NAME/K,LEVEL/N,ADD/S";
  48.  
  49. struct TA_Args {
  50.   STRPTR  DRIVE;
  51.   STRPTR  FILE;
  52.   ULONG  *START;
  53.   ULONG  *END;
  54.   ULONG   WRITE;
  55.   ULONG   ZLIB;
  56.   ULONG   GZIP;
  57.   ULONG   PKZIP;
  58.   STRPTR  NAME;
  59.   ULONG  *LEVEL;
  60.   ULONG   ADD;
  61. };
  62.  
  63.  
  64. /*----------------------------*/
  65. /* Standatd Input/Ouput/Error */
  66. /*----------------------------*/
  67.  
  68. BPTR StdIn, StdOut, StdErr;
  69. STRPTR ProgName;
  70.  
  71. /*
  72. ** Set handles for DOS Library StdIO functions, as well as
  73. ** find our command name.
  74. */
  75. void setStdIO (void)
  76. {
  77.   struct Process *Proc = (struct Process *)FindTask (NULL);
  78.   
  79.   StdIn  = Proc->pr_CIS;  /* Quicker than Input() or Output() */
  80.   StdOut = Proc->pr_COS;
  81.   StdErr = Proc->pr_CES;
  82.   if (!StdErr)
  83.   {
  84.     /* Use the file handle from the file number of stdio FILE stderr */
  85.     StdErr = (BPTR) fdtofh (fileno (stderr));
  86.     if (!StdErr) StdErr = StdOut;
  87.   }
  88. }
  89.  
  90.  
  91. /*-------------------------------------------*/
  92. /* Global variables and associated functions */
  93. /*-------------------------------------------*/
  94.  
  95. #ifdef COMPILE_RT
  96. /* z.library base pointer */
  97. struct Library *ZBase;
  98. #endif /* COMPILE_RT */
  99.  
  100. /* args */
  101. struct RDArgs  *rdargs;
  102.  
  103. /* files */
  104. BPTR   ADFile;
  105. STRPTR DOSDev;
  106.  
  107. /* io */
  108. struct MsgPort *diskPort;
  109. struct IOExtTD *diskReq;
  110. BOOL   TDOpen;
  111.  
  112.  
  113. /*
  114. ** Initialise all globals to their default values.
  115. */
  116. void initGlobals (void)
  117. {
  118. #ifdef COMPILE_RT
  119.   ZBase = NULL;
  120. #endif /* COMPILE_RT */
  121.   
  122.   setStdIO();
  123.   
  124.   rdargs = NULL;
  125.   
  126.   ADFile = NULL;
  127.   DOSDev = NULL;
  128.   
  129.   diskPort = NULL;
  130.   diskReq = NULL;
  131.   TDOpen = FALSE;
  132.   
  133.   /* Initialise other modules */
  134.   rd_TrackBuf = NULL;   /* In read_disk.c  */
  135.   wd_TrackBuf = NULL;   /* In write_disk.c */
  136. #ifndef COMPILE_LITE
  137.   dd_TrackBuf = NULL;   /* In defl_disk.c  */
  138.   dd_FileBuf  = NULL;   /* In defl_disk.c  */
  139.   id_FileBuf  = NULL;   /* In infl_disk.c  */
  140.   id_TrackBuf = NULL;   /* In infl_disk.c  */
  141. #endif /* COMPILE_LITE */
  142. }
  143.  
  144.  
  145. /*
  146. ** Exit cleanly.
  147. */
  148. void cleanExit (ULONG rc, LONG rc2)
  149. {
  150.   /* Cleanup other modules */
  151.   if (rd_TrackBuf) FreeVec (rd_TrackBuf);   /* In read_disk.c  */
  152.   if (wd_TrackBuf) FreeVec (wd_TrackBuf);   /* In write_disk.c */
  153. #ifndef COMPILE_LITE
  154.   if (dd_TrackBuf) FreeVec (dd_TrackBuf);   /* In defl_disk.c  */
  155.   if (dd_FileBuf)  FreeVec (dd_FileBuf);    /* In defl_disk.c  */
  156.   if (id_FileBuf)  FreeVec (id_FileBuf);    /* In infl_disk.c  */
  157.   if (id_TrackBuf) FreeVec (id_TrackBuf);   /* In infl_disk.c  */
  158. #endif /* COMPILE_LITE */
  159.   
  160.   if (TDOpen)
  161.   {
  162.     /* Flush any buffers that may be unwritten */
  163.     diskReq->iotd_Req.io_Command = CMD_UPDATE;
  164.     diskReq->iotd_Req.io_Flags = 0;
  165.     DoIO ((struct IORequest *)diskReq);
  166.     
  167.     /* Turn off the motor */
  168.     diskReq->iotd_Req.io_Command = TD_MOTOR;
  169.     diskReq->iotd_Req.io_Flags = 0;
  170.     diskReq->iotd_Req.io_Length = 0;
  171.     DoIO ((struct IORequest *)diskReq);
  172.     
  173.     CloseDevice ((struct IORequest *)diskReq);
  174.   }
  175.   
  176.   if (diskReq) DeleteIORequest ((APTR)diskReq);
  177.   if (diskPort) DeleteMsgPort (diskPort);
  178.   
  179.   if (DOSDev) Inhibit (DOSDev,FALSE);
  180.   if (ADFile) Close (ADFile);
  181.   
  182.   if (rdargs) FreeArgs (rdargs);
  183.   
  184.   /* Turn the cursor on */
  185.   FPuts (StdOut, "\033[1 p"); 
  186.   Flush (StdOut);
  187.   
  188. #ifdef COMPILE_RT
  189.   if (ZBase) CloseLibrary (ZBase);
  190. #endif /* COMPILE_RT */
  191.   
  192.   SetIoErr (rc2);
  193.   exit (rc);
  194. }
  195.  
  196.  
  197. /*---------------*/
  198. /* Main function */
  199. /*---------------*/
  200. int main (int argc, char *argv[])
  201. {
  202.   struct TA_Args taArgs = {NULL, NULL, NULL, NULL, 
  203.                            FALSE, FALSE, FALSE, FALSE, NULL, NULL, FALSE};
  204.   struct ADF_Packet adf_packet = {NULL, NULL, NULL, NULL, NULL, NULL};
  205.   
  206.   LONG   diskUnit;
  207.   STRPTR ADFileName, origName;
  208.   ULONG  startTrack = 0, endTrack = 159, cLevel = 6;
  209.   ULONG  fileType = 0;
  210.   
  211.   BOOL   WriteDisk = FALSE;
  212.   LONG   ADFOpenMode = MODE_NEWFILE;  /* Default is to write to file */
  213.   BYTE   TDError;
  214.   LONG   DOSError;
  215.   
  216.   
  217.   /* Initialise any global data */
  218.   initGlobals();
  219.   ProgName = FilePart (argv[0]);
  220.   
  221.   
  222. #ifdef COMPILE_RT
  223.   /* Open z.library */
  224.   ZBase = OpenLibrary ("z.library",0);
  225.   if (!ZBase)
  226.   {
  227.     FPrintf (StdErr, "%s: Couldn't open z.library.\n", ProgName);
  228.     cleanExit (RETURN_FAIL, NULL);
  229.   }
  230. #endif /* COMPILE_RT */
  231.   
  232.   /* Analyse arguments */
  233.   rdargs = ReadArgs (TA_Template, (ULONG *)&taArgs, NULL);
  234.   if (!rdargs)
  235.     outputUsage();
  236.   
  237.   diskUnit = Name2Unit (taArgs.DRIVE);
  238.   ADFileName = taArgs.FILE;
  239.   if (taArgs.START) startTrack = *(taArgs.START) * 2;
  240.   if (taArgs.END) endTrack = *(taArgs.END) * 2 + 1;
  241.   if (taArgs.WRITE) WriteDisk = TRUE;
  242.   
  243. #ifdef COMPILE_LITE
  244.   if ((taArgs.ZLIB) || (taArgs.GZIP) || (taArgs.PKZIP) || 
  245.       (taArgs.NAME) || (taArgs.LEVEL) || (taArgs.ADD))
  246.   {
  247.     FPrintf (StdErr, "%s: Options ZLIB, GZIP, PKZIP, NAME, LEVEL and ADD\n",
  248.                      ProgName);
  249.     FPuts (StdErr, " are disabled in Lite version.\n");
  250.     cleanExit (RETURN_FAIL, NULL);
  251.   }
  252. #else /* ifndef COMPILE_LITE */
  253.   if (taArgs.ZLIB) 
  254.     fileType = FT_ZLIB;
  255.   
  256.   else if (taArgs.GZIP)  
  257.     fileType = FT_GZIP;
  258.   
  259.   else if (taArgs.PKZIP) 
  260.     fileType = FT_PKZIP;
  261.   
  262.   origName = taArgs.NAME;
  263.   
  264.   if (taArgs.LEVEL) 
  265.     cLevel = *(taArgs.LEVEL);
  266.   
  267.   if ((taArgs.ADD) && (taArgs.PKZIP))
  268.     fileType = FT_PKZIP_ADD;
  269. #endif /* COMPILE_LITE */
  270.   
  271.   if (diskUnit < 0)
  272.   {
  273.     FPrintf (StdErr, "%s: Error - Invalid drive.\n",ProgName);
  274.     cleanExit (RETURN_FAIL, NULL);
  275.   }
  276.   if ((startTrack < 0) || (endTrack > 159) || (startTrack > endTrack))
  277.   {
  278.     FPrintf (StdErr, "%s: Invalid Start/End track.\n", ProgName);
  279.     cleanExit (RETURN_FAIL, NULL);
  280.   }
  281.   
  282.   /* Inhibit DOS access to the drive we'll be operating on */  
  283.   DOSDev = taArgs.DRIVE;
  284.   if ( !Inhibit (DOSDev, DOSTRUE) )
  285.   {
  286.     DOSError = IoErr();
  287.     FPrintf (StdErr, "%s: Error - Couldn't inhibit DOS access to %s.\n",
  288.                      ProgName, DOSDev);
  289.     DOSDev = NULL;
  290.     cleanExit (RETURN_FAIL, DOSError);
  291.   }
  292.   
  293.   /* Open the Amiga Disk File */
  294.   if (WriteDisk) ADFOpenMode = MODE_OLDFILE;  /* Reading from file */
  295.   else if (fileType == FT_PKZIP_ADD) ADFOpenMode = MODE_READWRITE;
  296.   ADFile = Open (ADFileName, ADFOpenMode);
  297.   if (!ADFile)
  298.   {
  299.     DOSError = IoErr();
  300.     FPrintf (StdErr, "%s: Couldn't open %s for %s - ", ProgName, ADFileName,
  301.                      (WriteDisk ? "input" : "output"));
  302.     reportDOSError (DOSError);
  303.     cleanExit (RETURN_FAIL, DOSError);
  304.   }
  305.   
  306.   /* Attempt to create a MsgPort */
  307.   diskPort = CreateMsgPort();
  308.   if (!diskPort)
  309.   {
  310.     FPrintf (StdErr, 
  311.              "%s: Error - Couldn't create a Message Port for TrackDisk IO.\n",
  312.              ProgName);
  313.     cleanExit (RETURN_FAIL, NULL);
  314.   };
  315.   
  316.   /* Attempt to create an IORequest */
  317.   diskReq = (struct IOExtTD *)
  318.             CreateIORequest (diskPort, sizeof (struct IOExtTD));
  319.   if (!diskReq)
  320.   {
  321.     FPrintf (StdErr,
  322.              "%s: Error - Couldn't create a Request for TrackDisk IO.\n",
  323.              ProgName);
  324.     cleanExit (RETURN_FAIL, NULL);
  325.   }
  326.   
  327.   /* Attempt to open trackdisk.device */
  328.   TDError = OpenDevice (TD_NAME, diskUnit, (struct IORequest *)diskReq,NULL);
  329.   if (TDError)
  330.   {
  331.     FPrintf (StdErr, "%s: Couldn't open trackdisk.device - ", ProgName);
  332.     reportTDError (TDError);
  333.     cleanExit (RETURN_FAIL, NULL);
  334.   }
  335.   else
  336.     TDOpen = TRUE;
  337.   
  338.   /* The trackdisk.device in now open and ready for use */
  339.   
  340.   /* Turn off the cursor */
  341.   FPuts (StdOut, "\033[0 p"); Flush (StdOut);
  342.   
  343.   /* Load packet */
  344.   adf_packet.diskReq    = diskReq;
  345.   adf_packet.diskUnit   = diskUnit;
  346.   adf_packet.ADFile     = ADFile;
  347.   adf_packet.ADFileName = ADFileName;
  348.   adf_packet.startTrack = startTrack;
  349.   adf_packet.endTrack   = endTrack;
  350.   
  351.   
  352.   if (WriteDisk)
  353.   {
  354.     /* We're reading from the file and writing to the disk */
  355. #ifndef COMPILE_LITE
  356.     fileType = getFileType (ADFile);
  357.     
  358.     if ((fileType == FT_ZLIB) || 
  359.         (fileType == FT_GZIP) ||
  360.         (fileType == FT_PKZIP))
  361.       inflDisk (&adf_packet, origName, fileType);
  362.     else
  363. #endif /* COMPILE_LITE */
  364.       writeDisk (&adf_packet);
  365.   }
  366.   else
  367.   {
  368.     /* We're reading from the disk and writing to the file */
  369. #ifndef COMPILE_LITE
  370.     if (fileType)
  371.       deflDisk (&adf_packet, cLevel, origName, fileType);
  372.     else
  373. #endif /* COMPILE_LITE */
  374.       readDisk (&adf_packet);
  375.   }
  376.   
  377.   
  378.   FPuts (StdOut, "\nDone.\n");
  379.   
  380.   /* Let's get outa here! */
  381.   cleanExit (RETURN_OK, NULL);
  382.   
  383.   /* Keep the compiler happy */
  384.   return 0;
  385. }
  386.